package cn.itcast.bos.service.transit.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.WayBillRepository;
import cn.itcast.bos.dao.transit.TransitInfoRepository;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.index.WayBillIndexRepository;
import cn.itcast.bos.service.transit.TransitInfoService;
@Service
@Transactional
public class TransitInfoServiceImpl implements TransitInfoService {
	@Resource
	private TransitInfoRepository transitInfoRespository; 
	
	@Resource
	private WayBillRepository wayBillRespository;
	
	@Resource
	private WayBillIndexRepository indexRepsitory;
	@Override
	public void creatTransits(String wayBillIds) {
		if (StringUtils.isNotBlank(wayBillIds)) {
			for (String wayBillId : wayBillIds.split(",")) {
				WayBill wayBill =  wayBillRespository.findOne(Integer.parseInt(wayBillId));
				//判断运单状态是否为待发货
				if (wayBill.getSignStatus() == 1) {
					//待发货
					//生成TransitInfo信息
					TransitInfo transitInfo = new TransitInfo();
					transitInfo.setWayBill(wayBill);
					transitInfo.setStatus("出入库中转");
					transitInfoRespository.save(transitInfo);
					
					//更改运单状态
					wayBill.setSignStatus(2);
					
					//同步索引库
					indexRepsitory.save(transitInfo.getWayBill());
				}
			}
		}
	}
	@Override
	public Page<TransitInfo> findAll(Pageable pageable) {
		return transitInfoRespository.findAll(pageable);
	}

}
